<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/facicon.ico?v=5.1.4" color="#222">





  <meta name="keywords" content="Java,">





  <link rel="alternate" href="/atom.xml" title="Key" type="application/atom+xml">






<meta name="description" content="String类的常用方法 开发中String是必用的，只依靠String的基本概念根本就无法满足开发需求，所以开发者必须熟练掌握String类的方法。所有String类的方法都可以通过java Doc 取得。 java SE 8 字符串与字符数组字符串就是一个字符数组，所以在String类里面支持有字符数组转换为字符串以及字符串变为字符的操作方法，这些操作方法定义如下：  public Strin">
<meta name="keywords" content="Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java String类的常用方法">
<meta property="og:url" content="https://blog.qipo.net/2018/01/31/java/index.html">
<meta property="og:site_name" content="Key">
<meta property="og:description" content="String类的常用方法 开发中String是必用的，只依靠String的基本概念根本就无法满足开发需求，所以开发者必须熟练掌握String类的方法。所有String类的方法都可以通过java Doc 取得。 java SE 8 字符串与字符数组字符串就是一个字符数组，所以在String类里面支持有字符数组转换为字符串以及字符串变为字符的操作方法，这些操作方法定义如下：  public Strin">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-06-03T02:37:45.202Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java String类的常用方法">
<meta name="twitter:description" content="String类的常用方法 开发中String是必用的，只依靠String的基本概念根本就无法满足开发需求，所以开发者必须熟练掌握String类的方法。所有String类的方法都可以通过java Doc 取得。 java SE 8 字符串与字符数组字符串就是一个字符数组，所以在String类里面支持有字符数组转换为字符串以及字符串变为字符的操作方法，这些操作方法定义如下：  public Strin">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://blog.qipo.net/2018/01/31/java/">





  <title>Java String类的常用方法 | Key</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Key</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.qipo.net/2018/01/31/java/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Key">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Key">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Java String类的常用方法</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-31T17:16:54+08:00">
                2018-01-31
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  3.2k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  13
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>String类的常用方法</p>
<p>开发中String是必用的，只依靠String的基本概念根本就无法满足开发需求，所以开发者必须熟练掌握String类的方法。所有String类的方法都可以通过java Doc 取得。</p>
<p><a href="https://docs.oracle.com/javase/8/docs/api/" target="_blank" rel="noopener">java SE 8</a></p>
<h1 id="字符串与字符数组"><a href="#字符串与字符数组" class="headerlink" title="字符串与字符数组"></a>字符串与字符数组</h1><p>字符串就是一个字符数组，所以在String类里面支持有字符数组转换为字符串以及字符串变为字符的操作方法，这些操作方法定义如下：</p>
<ul>
<li><code>public String(char[] value)</code>:构造类型，将字符数组中的所有内容变为字符串。</li>
<li><code>public String(char[] value,int offset,int count)</code>:构造类型，将部分字符数组中的内容变为字符串。</li>
<li><code>public char charAt(int index)</code>:普通类型，取得指定索引位置的字符，索引位置从零开始。</li>
<li><code>public char[] toCharArray()</code>：普通类型，将字符串变为字符数组返回。</li>
</ul>
<p>范例：观察<code>CharAt()</code>方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"Hello"</span>;</span><br><span class="line">        System.out.println(str.charAt(<span class="number">0</span>));<span class="comment">//H</span></span><br><span class="line">        <span class="comment">//取第一个位置的字符，如果超过该字符长度就会报错</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>字符串和字符数组的互相转换才是重点内容，那么下面来观察转换操作.</p>
<p>范例：字符串与字符数组的互相转换</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"helloworld"</span>;</span><br><span class="line">        <span class="keyword">char</span> data [] = str.toCharArray();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; data.length;x++)&#123;</span><br><span class="line">            data[x] -= <span class="number">32</span>;<span class="comment">//转大写字母</span></span><br><span class="line">            <span class="comment">//data[x] = (char)(data[x] - 32);</span></span><br><span class="line">            System.out.print(data[x]+<span class="string">"、"</span>);<span class="comment">//H、E、L、L、O、W、O、R、L、D、</span></span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println();</span><br><span class="line">        System.out.println(<span class="keyword">new</span> String(data));<span class="comment">//HELLOWORLD</span></span><br><span class="line">        <span class="comment">// 经过处理所有的小写字母全都转换为大写字字母了</span></span><br><span class="line">        System.out.println(<span class="keyword">new</span> String(data,<span class="number">5</span>,<span class="number">5</span>));<span class="comment">//WORLD</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>范例：现在有一个字符串，判断其是否由数字组成</p>
<p>因为现在不知道字符串的长度以及所包含的内容，最好的方法就是将其转变为字符数组，而后判断每个字符是否是“0”-“9”之间的内容，如果是则是数字，如果不是则不是数字。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String3</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"123456789"</span>;</span><br><span class="line">        System.out.println(isNumber(str) ? <span class="string">"由数字组成"</span> : <span class="string">"不是全由数字组成"</span>);<span class="comment">//由数字组成</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//一般而言，如果方法返回的是Boolean类型，往往以isXxx()命名</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isNumber</span><span class="params">(String str)</span></span>&#123;</span><br><span class="line">        <span class="keyword">char</span> data [] = str.toCharArray();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; data.length; x++)&#123;</span><br><span class="line">            <span class="keyword">if</span>(data[x] &lt; <span class="string">'0'</span> || data[x] &gt; <span class="string">'9'</span>)&#123;<span class="comment">//不是数字</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">false</span>; <span class="comment">//后面不需要再进行继续排查</span></span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;<span class="comment">//如果都没有错误，返回True</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>很多时候根据每一位进行判断的时候会有，如果都按照以上的方式操作的确会很麻烦 。</p>
<h1 id="字节与字符串"><a href="#字节与字符串" class="headerlink" title="字节与字符串"></a>字节与字符串</h1><p>字节更多的情况下是用于数据传输以及编码转换处理之中，在String类里面提供有对字节操作的支持。</p>
<ul>
<li><code>public String(byte[] bytes)</code>:构造方法，将字节数组变为字符串。</li>
<li><code>public String(byte[] bytes,int offset,int length)</code>:构造方法，将字节数组部分转换为字符串。</li>
<li><code>public byte[] getBytes()</code>:普通方法，将字符串以字节数组的形式返回。</li>
<li><code>public byte[] getBytes(String charsetName) throws UnsupportedEncodingException</code>:普通方法，编码转换处理。</li>
</ul>
<p>范例：实现字符串与字节数组的转换处理</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String4</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"helloworld"</span>;</span><br><span class="line">        <span class="keyword">byte</span> data [] = str.getBytes();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; data.length;x++)&#123;</span><br><span class="line">            data[x]-=<span class="number">32</span>;</span><br><span class="line">            System.out.print(data[x]+<span class="string">"、"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="keyword">new</span> String(data));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>通过程序可以发现，字节并不适合处理中文，而只有字符适合处理中文，并且按照程序的概念来讲，一个字符等于2个字节，字节只适合处理二进制数据。</p>
<h1 id="字符串的比较"><a href="#字符串的比较" class="headerlink" title="字符串的比较"></a>字符串的比较</h1><p>在之前使用过一个<code>equals()</code>的方法，该方法是可以进行区分大小写的相等判断，而除了这个方法之外，在String类里面还提供有如下的几个比较操作：</p>
<ul>
<li><code>public boolean equals(String anObject)</code>:普通，区分大小写的一个比较方法。</li>
<li><code>public boolean equalsIgnoreCase(String anothreString)</code>:普通，不区分大小写的比较。</li>
<li><code>public int compareTo(String anotherString)</code>:普通,比较两个字符串的大小关系。</li>
</ul>
<p>范例：不区分大小写比较</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String5</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"hello"</span>;</span><br><span class="line">        String str1 = <span class="string">"Hello"</span>;</span><br><span class="line">        System.out.println(str.equals(str1));<span class="comment">//false</span></span><br><span class="line">        System.out.println(str.equalsIgnoreCase(str1));<span class="comment">//true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在String类中<code>compareTo()</code>方法是一个最为重要的操作方法，该方法返回的是一个int型的数据，该数据会根据大小关系返回三类的内容：</p>
<ul>
<li>相等：返回0；</li>
<li>小于：返回的内容小于0；</li>
<li>大于：返回的内容大于0；</li>
</ul>
<p>范例：观察compareTo()的比较</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String6</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"A"</span>.compareTo(<span class="string">"a"</span>));<span class="comment">//-32</span></span><br><span class="line">        System.out.println(<span class="string">"a"</span>.compareTo(<span class="string">"A"</span>));<span class="comment">//32</span></span><br><span class="line">        System.out.println(<span class="string">"a"</span>.compareTo(<span class="string">"a"</span>));<span class="comment">//0</span></span><br><span class="line">        System.out.println(<span class="string">"ab"</span>.compareTo(<span class="string">"ac"</span>));<span class="comment">//-1</span></span><br><span class="line">        System.out.println(<span class="string">"王"</span>.compareTo(<span class="string">"凯"</span>));<span class="comment">//8604 没有意义</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>compareTo()</code>是唯一一个可以区分大小关系的方法，以后会有更加详细的讲解。</p>
<h1 id="字符串查找"><a href="#字符串查找" class="headerlink" title="字符串查找"></a>字符串查找</h1><p>从一个完整的字符串之中可以判断指定的内容是否存在，对于字符串的查找方法定义有很多。</p>
<ul>
<li><code>public boolean contains(String s)</code>:普通，判断一个子字符串是否存在。</li>
<li><code>public int indexOf(String str)</code>:普通，从头开始查找指定字符串的位置，查到了返回位置的开始索引，如果查不到返回-1；</li>
<li><code>public int indexOf(String str,int fromindex)</code>：普通，从指定位置开始查找子字符串的位置。</li>
<li><code>public int lastIndexOf(String str)</code>:普通，由后向前查找字符串的位置。</li>
<li><code>public int lastIndexOf(String str,int fromindex)</code>：普通，从指定位置由后向前查找。</li>
<li><code>public boolean startsWith(String prefix)</code>:普通，从头开始判断字符串是不是以指定的字符串开头。</li>
<li><code>public boolean startsWith(String prefix,int toffset)</code>:普通，判断指定位置是不是由指定字符串开头。</li>
<li><code>public boolean endsWith(String suffix)</code>:普通，判断是否以指定的子字符串结尾。</li>
</ul>
<p>范例：字符串查找，最方便的就是<code>contains</code>直接返回boolean类型。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String7</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"helloworld"</span>;</span><br><span class="line">        System.out.println(str.contains(<span class="string">"world"</span>)); <span class="comment">//true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>该判断形式是由JDK1.5之后开始追加的，那么在jdk1.5之前如果要实现与之类似的功能，就必须按照<code>indexOf()</code>的方法完成。</p>
<p>范例：使用<code>indexOf()</code>实现我们的位置查找</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String8</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"helloworld"</span>;</span><br><span class="line">        System.out.println(str.indexOf(<span class="string">"world"</span>));<span class="comment">//5 w开始的索引</span></span><br><span class="line">        System.out.println(str.indexOf(<span class="string">"java"</span>));<span class="comment">//-1 没有找到</span></span><br><span class="line">        <span class="keyword">if</span> (str.indexOf(<span class="string">"world"</span>) != -<span class="number">1</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">"找到了"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>但是现在都建议使用<code>contains()</code>这个方法完成。但是<code>indexOf()</code>需要注意的是，如果内容重复，他只能返回查找的第一个位置。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String9</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"helloworld"</span>;</span><br><span class="line">        System.out.println(str.indexOf(<span class="string">"l"</span>));<span class="comment">// 2</span></span><br><span class="line">        System.out.println(str.indexOf(<span class="string">"l"</span>,<span class="number">5</span>));<span class="comment">// 8</span></span><br><span class="line">        System.out.println(str.lastIndexOf(<span class="string">"l"</span>));<span class="comment">// 8</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在进行查找的时候往往会判断开头或结尾。</p>
<p>范例：判断开头或者结尾。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String10</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"**@@hello##"</span>;</span><br><span class="line">        System.out.println(str.startsWith(<span class="string">"**"</span>));<span class="comment">//true</span></span><br><span class="line">        System.out.println(str.startsWith(<span class="string">"@@"</span>,<span class="number">2</span>));<span class="comment">//true</span></span><br><span class="line">        System.out.println(str.endsWith(<span class="string">"##"</span>));<span class="comment">//true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>很多时候往往一些参数或利用一些标记做一些特殊处理，此时就需要使用<code>startsWith()</code>或者<code>endsWith()</code>了</p>
<h1 id="字符串替换"><a href="#字符串替换" class="headerlink" title="字符串替换"></a>字符串替换</h1><p>使用一个指定的新的字符串替换掉已有的字符串数据，字符串替换可以使用的方法如下：</p>
<ul>
<li><code>public String replaceAll(String regex,String replacement)</code>:普通，替换所有的指定内容</li>
<li><code>public String replaceFirst(String regex,String replacement)</code>:普通，替换首个内容</li>
</ul>
<p>范例：实现字符串的替换处理</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String11</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"helloworld"</span>;</span><br><span class="line">        System.out.println(str.replaceAll(<span class="string">"l"</span>,<span class="string">"_"</span>));<span class="comment">//he__owor_d</span></span><br><span class="line">        System.out.println(str.replaceFirst(<span class="string">"l"</span>,<span class="string">"_"</span>));<span class="comment">//he_loworld</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>字符串的替换操作与正则有关，后期还会对其进行分析。</p>
<h1 id="字符串拆分"><a href="#字符串拆分" class="headerlink" title="字符串拆分"></a>字符串拆分</h1><p>在整个字符串之中可以使用一个特定的字符来实现字符串的分割处理，也就是说可以将一个完整的字符串，按照指定的分割符划分为若干个子字符串。</p>
<ul>
<li><code>public String[] split(String regex)</code>:普通方法，将字符串全部拆分</li>
<li><code>public String[] split(String regex,int limit)</code>:普通方法，将字符串部分拆分，该数组长度就是limit极限</li>
</ul>
<p>范例：实现字符串的拆分处理</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String12</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"hello world hello"</span>;</span><br><span class="line">        String result [] = str.split(<span class="string">" "</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; result.length;x++)&#123;</span><br><span class="line">            System.out.println(result[x]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>范例：实现字符串的部分拆分</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String12</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"hello world hello"</span>;</span><br><span class="line">        String result [] = str.split(<span class="string">" "</span>,<span class="number">2</span>);<span class="comment">//会被拆分为2个元素即：hello 和 world hello</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; result.length;x++)&#123;</span><br><span class="line">            System.out.println(result[x]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>以上的拆分形式都很容易，如果发现有些内容无法拆分开，就需要使用<code>\\</code>转义。</p>
<p>范例：拆分ip地址</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String13</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"192.168.1.1"</span>;</span><br><span class="line">        String result [] = str.split(<span class="string">"\\."</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; result.length;x++)&#123;</span><br><span class="line">            System.out.println(result[x]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在以后实际开发之中，经常会出现这样的拆分模式<code>姓名：年龄|姓名：年龄|......</code>.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String14</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"kai:23|lei:24"</span>;</span><br><span class="line">        String result [] = str.split(<span class="string">"\\|"</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; result.length;x++)&#123;</span><br><span class="line">            String temp [] = result[x].split(<span class="string">":"</span>);</span><br><span class="line">            System.out.println(temp[<span class="number">0</span>] + <span class="string">"="</span> + temp[<span class="number">1</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个代码在以后的开发之中会经常出现，该程序必须会操作。</p>
<h1 id="字符串截取"><a href="#字符串截取" class="headerlink" title="字符串截取"></a>字符串截取</h1><p>从一个完整的字符串之中截取出部分内容，那么对于字符串的截取有以下几种方法：</p>
<ul>
<li><code>public String substring(int beginIndex)</code>:普通方法，从指定索引截取到结尾</li>
<li><code>public String substring(int beginIndex,int endIndex)</code>:普通方法，截取字符串部分内容</li>
</ul>
<p>范例:观察字符串的截取操作</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String15</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"helloworld"</span>;</span><br><span class="line">        System.out.println(str.substring(<span class="number">5</span>));<span class="comment">//world</span></span><br><span class="line">        System.out.println(str.substring(<span class="number">0</span>,<span class="number">5</span>));<span class="comment">//hello</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>千万记住了，程序中的字符串截取索引从0开始，而且只能够设置正整数，不能够使用负数</p>
<h1 id="其他操作方法"><a href="#其他操作方法" class="headerlink" title="其他操作方法"></a>其他操作方法</h1><p>在String类里面也有定义比较小的操作方法。</p>
<ul>
<li><code>public String trim()</code>:普通方法，去掉字符串之中的左右空格。</li>
<li><code>public String toUpperCase()</code>:普通方法，字符串转大写</li>
<li><code>public String toLowerCase()</code>:普通方法，字符串转小写</li>
<li><code>public String intern()</code>:普通方法，字符串入池</li>
<li><code>public String concat(String str)</code>:普通方法，等同于<code>+</code></li>
<li><code>public int length()</code>:普通方法，取得字符串的长度</li>
<li><code>public boolean isEmpty()</code>:普通方法，判断是否为空字符串（但不是null，而是长度为0）</li>
</ul>
<p>范例：观察<code>trim()</code>方法的使用。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String16</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"   helloworld   "</span>;</span><br><span class="line">        System.out.println(<span class="string">"【"</span>+str.trim()+<span class="string">"】"</span>);<span class="comment">//【helloworld】</span></span><br><span class="line">        System.out.println(<span class="string">"【"</span>+str+<span class="string">"】"</span>);<span class="comment">//【   helloworld   】</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>范例：字符串转换大小写</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String17</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"hello &amp;^)#0948 WORLD"</span>;</span><br><span class="line">        System.out.println(str.toUpperCase());<span class="comment">//HELLO &amp;^)#0948 WORLD</span></span><br><span class="line">        System.out.println(str.toLowerCase());<span class="comment">//hello &amp;^)#0948 world</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用这两个函数之后，如果不是字母的内容将不进行字母大小写的转换，实际上就少了用户的判断。</p>
<p>范例：观察<code>concat()</code>方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String18</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String str = <span class="string">"helloworld"</span>;</span><br><span class="line">        String str1 = <span class="string">"hello"</span>.concat(<span class="string">"world"</span>);</span><br><span class="line">        System.out.println(str1);<span class="comment">//helloworld</span></span><br><span class="line">        System.out.println(str1 == str);<span class="comment">//false</span></span><br><span class="line">        System.out.println(str == str1.intern());<span class="comment">//true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>范例:观察<code>length()</code>方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String19</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"helloworld"</span>.length());<span class="comment">//10</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在数组上有一个格式，<code>数组名称.length</code>，但是在String类中，<code>length()</code>是一个方法，方法必须通过对象才可以调用，而且方法后面一定有<code>()</code>存在。</p>
<p>范例：观察<code>isEmpty()</code>方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String20</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"hello"</span>.isEmpty());<span class="comment">//false</span></span><br><span class="line">        System.out.println(<span class="string">""</span>.isEmpty());<span class="comment">//true</span></span><br><span class="line">        System.out.println(<span class="keyword">new</span> String().isEmpty());<span class="comment">//true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在String类中唯一遗憾的是没有提供首字母大写的方法，所以如果要想使用就必须自己实现。</p>
<p>范例：实现首字母大写</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 字符串方法;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">String21</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        String name = <span class="string">"kai"</span>;</span><br><span class="line">        System.out.println(initcap(name));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">initcap</span><span class="params">(String str)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// isEmpty()和equals()</span></span><br><span class="line">        <span class="keyword">if</span> (str == <span class="keyword">null</span> || <span class="string">""</span>.equals(str))&#123;</span><br><span class="line">            <span class="keyword">return</span> str;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (str.length() &gt; <span class="number">1</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> str.substring(<span class="number">0</span>,<span class="number">1</span>).toUpperCase() + str.substring(<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str.toUpperCase();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这种首字母大写的操作在实际开发之中使用频率很高。</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Key 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Key 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Java/" rel="tag"># Java</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/01/27/java/" rel="next" title="Java String类的基本特点">
                <i class="fa fa-chevron-left"></i> Java String类的基本特点
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/02/13/java/" rel="prev" title="Java 引用传递分析">
                Java 引用传递分析 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="Key">
            
              <p class="site-author-name" itemprop="name">Key</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">107</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/keyarea" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:wxkayle@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#字符串与字符数组"><span class="nav-number">1.</span> <span class="nav-text">字符串与字符数组</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#字节与字符串"><span class="nav-number">2.</span> <span class="nav-text">字节与字符串</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#字符串的比较"><span class="nav-number">3.</span> <span class="nav-text">字符串的比较</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#字符串查找"><span class="nav-number">4.</span> <span class="nav-text">字符串查找</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#字符串替换"><span class="nav-number">5.</span> <span class="nav-text">字符串替换</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#字符串拆分"><span class="nav-number">6.</span> <span class="nav-text">字符串拆分</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#字符串截取"><span class="nav-number">7.</span> <span class="nav-text">字符串截取</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#其他操作方法"><span class="nav-number">8.</span> <span class="nav-text">其他操作方法</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-snowflake-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Key</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">190.8k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
